package logic

import (
	"fmt"
	"strconv"
	"time"

	"gitee.com/leicc/go-app-core/logics"
	"gitee.com/leicc/go-app-core/models/im"
	"gitee.com/leicc/go-orm"
	"gitee.com/leicc/go-orm/log"
)

type RedisSt struct {
}

//初始化数据基础信息
var RState *RedisSt = nil
func NewState() *RedisSt {
	RState = &RedisSt{}
	return RState
}

//获取用户ID获取唯一的key信息
func (c RedisSt)UserFormat(appid, userid int64) string {
	str := fmt.Sprintf("im-%d@userid_%d", appid, userid)
	return str
}

//用户登录注册到redis当中-长连接的时候验证
func (c RedisSt)Register(appid, userid int64, vkey string) string {
	ckey  := c.UserFormat(appid, userid)
	wsUri := GConfig.ImSrv
	hSet  := map[string]interface{}{"vkey":vkey, "mqueue": GConfig.MQueue, "wsUri":wsUri}
	logics.CRedis.HMSet(ckey, hSet) //设置值到redis当中
	return wsUri
}

//验证请求连接的token是否合法
func (c RedisSt)Vkey(appid, userid int64) string {
	ckey := c.UserFormat(appid, userid)
	vkey := logics.CRedis.HGet(ckey, "vkey").Val()
	return vkey
}

//验证请求连接的token是否合法
func (c RedisSt)Token(appid, userid int64, token string) bool {
	ckey := c.UserFormat(appid, userid)
	vkey := logics.CRedis.HGet(ckey, "vkey").Val()
	if vkey != token {
		return false
	}
	hSet := map[string]interface{}{"mqueue": GConfig.MQueue, "state":1}
	logics.CRedis.HMSet(ckey, hSet) //设置值到redis当中
	log.Write(log.INFO, "redis设置用户在线userid={", userid, "}")
	return true
}

//验证请求的用户是否在线
func (c RedisSt)MQueue(appid, userid int64) string {
	ckey := c.UserFormat(appid, userid)
	MQtopic := logics.CRedis.HGet(ckey,"mqueue").Val()
	return MQtopic
}

//验证请求连接的token是否合法
func (c RedisSt)OffLine(appid, userid int64) bool {
	ckey := c.UserFormat(appid, userid)
	logics.CRedis.HSet(ckey, "state", 0)
	log.Write(log.INFO, "redis set offline userid={", userid, "}")
	return true
}

//验证请求的用户是否在线
func (c RedisSt)IsOnline(appid, userid int64) bool {
	ckey := c.UserFormat(appid, userid)
	state, err := logics.CRedis.HGet(ckey,"state").Int()
	if err == nil && state == 1 {
		return true
	}
	return false
}

//设置用户禁言的操作
func (c RedisSt)SetNoSpeak(appid, userid int64, exp int64) int64 {
	ckey := c.UserFormat(appid, userid)
	if exp < time.Now().Unix() && exp > 0 {
		exp += time.Now().Unix()
	}
	logics.CRedis.HSet(ckey, "mutedExp", exp)
	return exp
}

//判定用户禁言的操作
func (c RedisSt)IsNoSpeak(appid, userid int64) bool {
	ckey := c.UserFormat(appid, userid)
	muteExp := logics.CRedis.HGet(ckey, "mutedExp").Val()
	if len(muteExp) > 1 {
		exp, err := strconv.ParseInt(muteExp, 10, 64)
		if err == nil && exp > time.Now().Unix() {
			return true
		}
	}
	return false
}

//加入或者离开频道的时候 顺带返回频道用户id
func (c RedisSt)JoinQuitChan(appid, userid, chnid int64, optype int8) {
	ckey := fmt.Sprintf("im-%d@chnid_%d", appid, chnid)
	if optype == 0 {//退出聊天室
		logics.CRedis.SRem(ckey, userid)
	} else {//加入聊天室
		logics.CRedis.SAdd(ckey, userid)
	}
}

//加入或者离开频道的时候 顺带返回频道用户id
func (c RedisSt)ChanMembers(appid, chnid int64) []string {
	ckey := fmt.Sprintf("im-%d@chnid_%d", appid, chnid)
	users := logics.CRedis.SMembers(ckey).Val()
	return  users
}

//判断当前userid 是否被touserid列为黑名单
func (c RedisSt) IsFilter(appid, userid, touserid int64) bool {
	sorm  := im.NewImFilter().SetModTable(touserid)
	oldid := sorm.GetValue(func(st *orm.QuerySt) string {
		st.Where("userid", touserid).Where("touserid", userid).Where("appid", appid)
		return st.GetWheres()
	}, "id").ToInt64()
	if oldid > 0 {
		return true
	}
	return false
}


